"""Build mpi abstraction libraries"""


def build_dpar(env, mpi_env):
    """Build MPI abstraction library"""

    senv = env.Clone()
    senv.compiler_setup()

    serial_lib = senv.d_library('dpar', ['dpar_stub.c'], LIBS=['pthread', 'dl'])
    senv.Install('$PREFIX/lib64/', serial_lib)

    if not mpi_env:
        return

    denv = mpi_env.Clone()

    mpi_lib = denv.d_library('dpar_mpi', ['dpar_mpi.c'])
    denv.Install('$PREFIX/lib64/', mpi_lib)
    return


def scons():
    """Execute build"""
    Import('env', 'base_env', 'base_env_mpi', 'prereqs')

    if not prereqs.test_requested():
        return

    env.AppendUnique(LIBPATH=[Dir('.')])
    base_env.AppendUnique(LIBPATH=[Dir('.')])
    env.d_add_build_rpath()
    base_env.d_add_build_rpath()
    if base_env_mpi:
        base_env_mpi.AppendUnique(LIBPATH=[Dir('.')])
        base_env_mpi.d_add_build_rpath()

    build_dpar(base_env, base_env_mpi)


if __name__ == "SCons.Script":
    scons()
